package com.zh.zhbootcommon.utils.common;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述: 工具类
 *
 * @author: yanglin
 * @Date: 2021-07-29-9:06
 * @Version: 1.0
 */
public class CommonUtils {

    /**
     * 用户登录有效时间
     */
    public final static long USER_LOGIN_EXPIRE = 12 * 60 * 60;

    /**
     * TOKEN 幂等性 有效时间
     */
    public final static long MP_TOKEN_EXPIRE = 10;

    /**
     * 当前时间的前1分钟的时间
     * 结果：newDate = 2020-12-24 13:45:40 Before=2020-12-24 13:44:40
     *
     * @return
     */
    public static String getOneMinuteBeforeTime() {
        Date newDate = DateUtil.offset(DateUtil.date(), DateField.MINUTE, -1);
        String startTime = DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 当前时间的前1分钟的时间
     *
     * @param now
     * @return
     */
    public static String getOneMinuteBeforeTime(String now) {
        Date date = DateUtil.parse(now);
        Date newDate = DateUtil.offset(date, DateField.MINUTE, -1);
        String startTime = DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 当前时间的后1个分钟的时间
     * 结果：newDate = 2020-12-24 13:45:40 Before=2020-12-24 13:46:40
     *
     * @return
     */
    public static String getOneMinuteAfterTime() {
        Date newDate = DateUtil.offset(DateUtil.date(), DateField.MINUTE, 1);
        String startTime = DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 当前时间的后1个分钟的时间
     * 结果：newDate = 2020-12-24 13:45:40 Before=2020-12-24 13:46:40
     *
     * @param now
     * @return
     */
    public static String getOneMinuteAfterTime(String now) {
        Date date = DateUtil.parse(now);
        Date newDate = DateUtil.offset(date, DateField.MINUTE, 1);
        String startTime = DateUtil.format(newDate, "yyyy-MM-dd HH:mm:ss");
        return startTime;
    }

    /**
     * 随机生成验证码(数字英文字母)
     *
     * @return
     */
    public static String getVerifyCode(int length) {
        String strCode = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(strCode.length());
            char c = strCode.charAt(index);
            result += c;
        }
        return result;

    }

    /**
     * 随机生成验证码(纯数字数字)
     *
     * @return
     */
    public static String getVerifyNumCode(int length) {
        String strCode = "123456789012345678901234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(strCode.length());
            char c = strCode.charAt(index);
            result += c;
        }
        return result;

    }

    /**
     * 获取用户ip
     *
     * @param request
     * @return
     * @Autowired
     */
    public static String getRemortIP(HttpServletRequest request) {
        if (null == request.getHeader("x-forwarded-for")) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }

    /**
     * 拼接被点赞的用户id和点赞的人的id作为key。格式 222222::333333
     *
     * @param likedUserId 被点赞的人id
     * @param likedPostId 点赞的人的id
     * @return
     */
    public static String getLikedKey(String likedUserId, String likedPostId) {
        StringBuilder builder = new StringBuilder();
        builder.append(likedUserId);
        builder.append("::");
        builder.append(likedPostId);
        return builder.toString();
    }

}
